package entries;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;

import Models.Ingredient;
import Views.IngredientFrame;

public class IngredientEntry extends JPanel implements ActionListener{
	private JLabel name;
	private JTextArea description;
	private JButton minus;
	
	private IngredientFrame parent;
	
	public IngredientEntry(IngredientFrame parent, Ingredient ing) {
		this.parent = parent;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.rowWeights = new double[]{0.0, 1.0};
		gridBagLayout.columnWeights = new double[]{1.0};
		this.setLayout(gridBagLayout);
		name = new JLabel(ing.getIngredientName());
		name.setPreferredSize(new Dimension(150,30));
		name.setAlignmentX(LEFT_ALIGNMENT);
		GridBagConstraints gbcName = new GridBagConstraints();
		gbcName.insets = new Insets(25, 0, 0, 0);
		gbcName.anchor = GridBagConstraints.WEST;
		gbcName.weightx = 1;
		gbcName.gridx = 0;
		gbcName.gridy = 0;
		this.add(name, gbcName);
		
		minus = new JButton("-");
		minus.addActionListener(this);
		GridBagConstraints gbcMS = new GridBagConstraints();
		gbcMS.insets = new Insets(0, 0, 0, 0);
		gbcMS.anchor = GridBagConstraints.WEST;
		gbcMS.weightx = 1;
		gbcMS.gridx = 1;
		gbcMS.gridy = 0;
		this.add(minus, gbcMS);
		
		JLabel descTitle = new JLabel("Description:");
		GridBagConstraints gbcDT = new GridBagConstraints();
		gbcDT.insets = new Insets(0, 0, 0, 0);
		gbcDT.anchor = GridBagConstraints.WEST;
		gbcDT.weightx = 1;
		gbcDT.gridx = 0;
		gbcDT.gridy = 1;
		this.add(descTitle, gbcDT);
		
		JTextArea descArea = new JTextArea(ing.getDescription());
		descArea.setEditable(false);
		GridBagConstraints gbcTA = new GridBagConstraints();
		gbcTA.fill = GridBagConstraints.BOTH;
		gbcTA.insets = new Insets(0, 0, 0, 0);
		gbcTA.gridx = 0;
		gbcTA.gridy = 2;
		add(descArea, gbcTA);
		
		JSeparator sep = new JSeparator();
		GridBagConstraints gbcSep = new GridBagConstraints();
		gbcSep.fill = GridBagConstraints.BOTH;
		gbcSep.insets = new Insets(0, 0, 0, 0);
		gbcSep.gridx = 0;
		gbcSep.gridy = 3;
		add(sep, gbcSep);
		//description = new JLabel
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		int option = JOptionPane.showConfirmDialog(null,"Delete Ingredient?", "Delete Ingredient?", JOptionPane.YES_NO_OPTION);
		if(option == JOptionPane.YES_OPTION) {
			parent.removeEntry(this);
		}
	}
}
